var app=getApp();
Page({
  data: {
    orderId:'',
    receivedAmount:'',
    typeOne: true,
    typeTwo: false,
    cancelReason: '',
    refundPrice: '',
    disableBtn: true,
    refundamount:'',
    refundType: '1',
    cancelReasonNum: 0,
    flag:false
  },
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId,
      receivedAmount: options.receivedAmount
    })
  },
  //退款类型选择
  radioChange: function (event) {
    var _this = this;
    _this.setData({
      refundType: event.detail.value
    })
    if (event.detail.value == 1) {
      _this.setData({
        typeOne: true,
        typeTwo: false,
        refundPrice: ''
      })
    } else if (event.detail.value == 2 || event.detail.value == 3) {
      _this.setData({
        typeOne: false,
        typeTwo: true
      })
    } 
    this.sureAble();
  },
  //获取取消原因
  cancelReason: function (event) {
    this.setData({
      cancelReason: event.detail.value,
      cancelReasonNum: event.detail.value.length
    })
    this.sureAble();
  },
  //获取退款金额
  refundPrice: function (event) {
    this.setData({
      refundPrice: event.detail.value
    })
    this.sureAble();
  },
  //确定按钮高亮判断
  sureAble: function () {
    var _this = this;
    if (_this.data.typeTwo) {
      if (_this.data.cancelReason.length > 0 && _this.data.refundPrice >0) {
        this.setData({
          disableBtn: false
        })
      } else {
        this.setData({
          disableBtn: true
        })
      }
    } else {
      if (_this.data.cancelReason.length > 0) {
        this.setData({
          disableBtn: false
        })
      } else {
        this.setData({
          disableBtn: true
        })
      }
    }

  },
  //退款金额取值
  refundamount:function () {
    var _this = this;
    if (_this.data.typeOne) {
      _this.setData({
        refundamount: _this.data.receivedAmount
      })
    } else if (_this.data.typeTwo) {
      _this.setData({
        refundamount: _this.data.refundPrice
      })
    } 
  },
  //点击确定
  refundOrder: function () {
    var _this = this;
    _this.refundamount()
    if(_this.data.flag){
      return;
    }else{
      _this.setData({
        flag: true
      })
      app.request({
        url: app.globalData.apiUrl +'/mobileOfficing/api/roomReservation/verifyRefund',
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'sessionId': wx.getStorageSync(app.globalData.sessionId)
        },
        data: {
          orderId: _this.data.orderId,
          refundCause: _this.data.cancelReason,
          refundAmount: _this.data.refundamount,
          receivedAmount: _this.data.receivedAmount,
          refundType: _this.data.refundType
        },
        success: function (res) {
          console.log(res)
          if (res.data.result) {
            wx.showToast({
              title: '退款成功',
              icon: 'success',
              duration: 1000
            })
            setTimeout(function () {
              wx.redirectTo({
                url: '../details/details?orderId=' + _this.data.orderId
              })
            }, 2500)

          } else {
            _this.setData({
              flag: false
            })
            var msg = res.data.msg;
            wx.showModal({
              title: '提示',
              content: msg,
              showCancel: false,
              confirmText: '知道了',
              success: function (res) {
                if (res.confirm) {
                  if (msg.indexOf('金额') >= 0 || msg.indexOf('请求参数') >= 0) {
                    console.log('用户点击确定')
                  } else {
                    wx.redirectTo({
                      url: '../details/details?orderId=' + _this.data.orderId
                    })
                  }
                }
              }
            })
          }
        }
      })
    }
    
  },
  //点击返回详情页
  goBackDetails: function () {
    var _this = this;
    wx.redirectTo({
      url: '../details/details?orderId=' + _this.data.orderId
    })
  }


})